package com.du.cloud.oauth.controller;

import com.du.cloud.oauth.model.Result;
import com.du.cloud.oauth.model.emun.ResultCode;
import com.du.cloud.oauth.model.info.UserDetail;
import com.du.cloud.oauth.config.MyUserDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Desc:
 * Author: dushuang
 * Date 2019/12/30 16:51
 */
@Controller
@RequestMapping(value = "auth/api")
@Slf4j
public class AuthController {
    @Autowired
    private MyUserDetailsService userDetailService;

    @Autowired
    private ConsumerTokenServices consumerTokenServices;

    @GetMapping(value = "user")
    @ResponseBody
    public UserDetail user(OAuth2Authentication authentication) {
        log.info("{}", authentication);
        return (UserDetail)((OAuth2AuthenticationDetails) authentication.getDetails()).getDecodedDetails();
    }

    @DeleteMapping(value = "exit")
    public Result revokeToken(String access_token) {
        Result result = new Result();
        if (consumerTokenServices.revokeToken(access_token)) {
            result.setCode(ResultCode.SUCCESS.code);
            result.setMsg("注销成功");
        } else {
            result.setCode(ResultCode.FAILED.code);
            result.setMsg("注销失败");
        }
        return result;
    }
}
